﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MillerApp.UserControls;
using MillerApp.UserControls.Enums;
using MillerApp.DataContract.MillerDB;
using MillerApp.DataAccess.MillerDB;

namespace MillerApp.Stock.Query
{
    public partial class FundflowQuery : MillerApp.UserControls.SubFormBase
    {
        public FundflowQuery()
        {
            InitializeComponent();
        }

        //默认排序字段和顺次 [默认是按照证券代码升序排列]
        string column = "tradeday";
        SortDirection direction = SortDirection.DESC;

        private void FundflowQuery_Load(object sender, EventArgs e)
        {
            //获取当前用户
            //currentUser = Common.Common.GetCurrentUser(this);
            //dataGridView1.ShowCellToolTips = true;
            this.dataGridView1.AutoGenerateColumns = false;
            this.queryActions1.PagerControl = this.dataPagerControl1;
            this.queryActions1.OnQueryingData += new QueryingDataEventHandler(queryActions1_OnQueryingData);
            this.queryActions1.GetConditions += new GetStringDelegate(queryActions1_GetConditions);
            this.queryActions1.GetSortExpression += new GetSortExpression(queryActions1_GetSortExpression);
            dataGridView1.ColumnHeaderMouseClick += delegate(object sender2, DataGridViewCellMouseEventArgs e2)
            {
                column = this.queryActions1.CurrentSortColumn;
                direction = this.queryActions1.CurrentSortDirection;
                string columnName = dataGridView1.Columns[e2.ColumnIndex].Name;
                if (columnName == column)
                {
                    direction = direction == SortDirection.ASC ? SortDirection.DESC : SortDirection.ASC;
                }
                else
                {
                    column = columnName;
                }
                this.queryActions1.OnQueryData();
            };
        }

        SortExpression queryActions1_GetSortExpression()
        {
            SortExpression exp = new SortExpression();
            exp.SortColumn = column;
            exp.SortDirection = direction;
            return exp;
        }

        string queryActions1_GetConditions()
        {
            string conditions = string.Empty;
            if (string.IsNullOrEmpty(this.dpkerStart.Text) == false)
            {
                conditions += " and tradeday>= '" + dpkerStart.Value.Date.ToString("yyyy-MM-dd") + "'";
            }
            if (string.IsNullOrEmpty(this.dpkerEnd.Text) == false)
            {
                conditions += " and tradeday<= '" + dpkerEnd.Value.Date.ToString("yyyy-MM-dd") + "'";
            }
            return conditions;
        }

        void queryActions1_OnQueryingData(string conditions, string sortExpression, int pageIndex, int pageSize)
        {
            //查询
            int recordCount = 0;
            List<FundflowEntity> list = FundflowManager.GetFundflowsByCond(conditions, sortExpression, pageIndex, pageSize, out recordCount);

            this.dataGridView1.DataSource = list;
            this.queryActions1.RecorderCount = recordCount;
            SetDataGridStyle(dataGridView1);
        }

        private void SetCellForeColor(DataGridView gridView, int row, int cell, Color color)
        {
            gridView.Rows[row].Cells[cell].Style.ForeColor = color;
        }

        private void SetDataGridStyle(DataGridView gridView)
        {
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                //1. 设置字体颜色
                #region 设置字体颜色
                List<int> setRedCells = new List<int>();
                object obj = gridView.Rows[i].DataBoundItem;
                if (obj == null)
                {
                    continue;
                }
                FundflowEntity dataItem = obj as FundflowEntity;
                if (dataItem == null)
                {
                    continue;
                }
                if (dataItem.netflow > 0)
                {
                    SetCellForeColor(gridView, i,4, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView, i, 4, Color.Green);
                }
                if (dataItem.inflowpfr.HasValue&&dataItem.inflowpfr > 0)
                {
                    SetCellForeColor(gridView, i, 4, Color.Red);
                }
                else
                {
                    SetCellForeColor(gridView, i, 5, Color.Green);
                }
                #endregion 设置字体颜色
               
            }
        }

        

    }
}
